using UnityEngine;
using System.Collections;

[System.Serializable]
public class PickupBatteryOnTrigger : MonoBehaviour {
	
	public BatteryLevelFlashlight batteryLevelScript;
	public AudioSource audioSource;
	public Texture icon;
	
	public AudioClip playOnTrigger;
	public bool onTrigger = false;
	
	private bool triggered = false;
	
	// Use this for initialization
	void Start () {
	}
	
	// Update is called once per frame
	void Update () {
		
	}
	
	void OnTriggerEnter(Collider other) {
		if (other.tag == "Player" && !triggered && onTrigger) {
			triggered = true;
			PickupBattery();
		}
	}
	
	void PickupBattery() {			
		while (audioSource.isPlaying) { /* Wait till the source is free to use */ }
		audioSource.PlayOneShot(playOnTrigger);
		
		Battery newBattery = new Battery();
		newBattery.icon = icon;
		newBattery.audio = audio.clip;
		newBattery.audioSource = audioSource;
		newBattery.batteryLevelScript = batteryLevelScript;
		Game.inventory.items[newBattery.index] = newBattery;
		
		Object.Destroy(gameObject);
	}
	
	void  OnMouseUpAsButton() { 
		PickupBattery();
	}
}
